package Utils.basicDataTypes;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * 基本数据类型工具类
 *
 * @author d11yu
 */
public final class BasicDataTypesUtils {
    /**
     * 在控制台输出所有的Node节点
     *
     * @param node   起始node节点
     * @param indent 缩进
     */
    public static void printNode(Node node, int indent) {
        for (int i = 0; i < indent; i++) {
            System.out.print("    ");
        }
        switch (node.getNodeType()) {
            case Node.DOCUMENT_NODE:
                System.out.println("Document: " + node.getNodeName());
                break;
            case Node.ELEMENT_NODE:
                System.out.println("Element: " + node.getNodeName());
                break;
            case Node.TEXT_NODE:
                System.out.println("Text: " + node.getNodeName() + " = " + node.getNodeValue());
                break;
            case Node.ATTRIBUTE_NODE:
                System.out.println("Attribute: " + node.getNodeName() + " = " + node.getNodeValue());
                break;
            default:
                System.out.println("NodeType: " + node.getNodeType() + ", NodeName: " + node.getNodeName());
                break;
        }
        if (node.hasAttributes()) {
            NamedNodeMap as = node.getAttributes();
            for (int i = 0; i < as.getLength(); i++) {
                printNode(as.item(i), indent + 1);
            }
        }
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            printNode(child, indent + 1);
        }
    }
}
